<?php
if(!defined('IN')) exit('Access Denied');

/**
 * 获得商品的属性和规格
 *
 * @access  public
 * @param   integer $goods_id
 * @return  array
 */
function get_goods_properties($goods_id,$db)
{
    /* 对属性进行重新排序和分组 */
    $sql = "SELECT attr_group ".
            "FROM `info_goods_type` AS gt, `info_goods` AS g ".
            "WHERE g.goods_id='$goods_id' AND gt.cat_id=g.goods_type";
    $grpOne = $db->fetch_one($sql);
	$grp = $grpOne['attr_group'];
    if (!empty($grp))
    {
        $groups = explode("\n", strtr($grp, "\r", ''));
    }

    /* 获得商品的规格 */
    $sql = "SELECT a.attr_id, a.attr_name, a.attr_group, a.is_linked, a.attr_type, ".
                "g.goods_attr_id, g.attr_value, g.attr_price " .
            'FROM `info_goods_attr` AS g ' .
            'LEFT JOIN `info_attribute` AS a ON a.attr_id = g.attr_id ' .
            "WHERE g.goods_id = '$goods_id' " .
            'ORDER BY a.sort_order, g.attr_price, g.goods_attr_id';
    $res = $db->fetch_all($sql);

    $arr['pro'] = array();     // 属性
    $arr['spe'] = array();     // 规格
    $arr['lnk'] = array();     // 关联的属性

    foreach ($res AS $row)
    {
        $row['attr_value'] = str_replace("\n", '<br />', $row['attr_value']);

        if ($row['attr_type'] == 0)
        {
            $group = (isset($groups[$row['attr_group']])) ? $groups[$row['attr_group']] : '商品属性';

            $arr['pro'][$group][$row['attr_id']]['name']  = $row['attr_name'];
            $arr['pro'][$group][$row['attr_id']]['value'] = $row['attr_value'];
        }
        else
        {
            $arr['spe'][$row['attr_id']]['attr_type'] = $row['attr_type'];
            $arr['spe'][$row['attr_id']]['name']     = $row['attr_name'];
            $arr['spe'][$row['attr_id']]['values'][] = array(
                                                        'label'        => $row['attr_value'],
                                                        'price'        => $row['attr_price'],
                                                        'format_price' => '',
                                                        'id'           => $row['goods_attr_id']);
        }

        if ($row['is_linked'] == 1)
        {
            /* 如果该属性需要关联，先保存下来 */
            $arr['lnk'][$row['attr_id']]['name']  = $row['attr_name'];
            $arr['lnk'][$row['attr_id']]['value'] = $row['attr_value'];
        }
    }

    return $arr;
}

/**
 * 获得商品的详细信息
 *
 * @access  public
 * @param   integer     $goods_id
 * @return  void
 */
function get_goods_info($goods_id,$db)
{
    $sql = 'SELECT * from `info_goods`'.
            "WHERE goods_id = '$goods_id' ";
    $row = $db->fetch_one($sql);

    if ($row !== false){
        return $row;
    }else{
        return false;
    }
}

/**
 * 获得指定分类的所有上级分类
 *
 * @access  public
 * @param   integer $cat    分类编号
 * @return  array
 */
function get_parent_cats($cat,$db)
{
    if ($cat == 0)
    {
        return array();
    }

    $arr = $db->fetch_all('SELECT cat_id, cat_name, parent_id FROM `info_category`');

    if (empty($arr))
    {
        return array();
    }

    $index = 0;
    $cats  = array();

    while (1)
    {
        foreach ($arr AS $row)
        {
            if ($cat == $row['cat_id'])
            {
                $cat = $row['parent_id'];

                $cats[$index]['cat_id']   = $row['cat_id'];
                $cats[$index]['cat_name'] = $row['cat_name'];

                $index++;
                break;
            }
        }

        if ($index == 0 || $cat == 0)
        {
            break;
        }
    }

    return $cats;
}

/**
 *  获取用户信息数组
 *
 * @access  public
 * @param
 *
 * @return array        $user       用户信息数组
 */
function get_user_info($id=0,$db)
{
    $sql  = 'SELECT u.user_id, u.email, u.user_name'.
            ' FROM `info_users` AS u ' .
            " WHERE u.user_id = '$id'";
    $user = $db->fetch_one($sql);

    $user['username']    = $user['user_name'];
	$user['user_avatar'] = substr($user['user_id'],0,2).'/'.substr($user['user_id'],2,2).'/'.substr($user['user_id'],4);
    return $user;
}

/**
 *  获取qq用户信息
*/
function get_qquser_info($id=0,$db){
	$sql = "select `nick`,`face` from `info_link_user` where `id`='$id'";
	$user = $db->fetch_one($sql);
	$user['username']    = empty($user['nick'])?'':$user['nick'];
	$user['user_avatar'] = empty($user['face'])?'':$user['face'];
	return $user;
}